#include <jni.h>
#include <string>
#include <android/log.h>
#include "x264.h"
#include "VideoChannel.h"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,"hugang",__VA_ARGS__)

VideoChannel *mVideoChannel = NULL;

extern "C"
JNIEXPORT void JNICALL
Java_com_example_myx264test_CameraUtil_native_1setVideoEncInfo(JNIEnv *env, jobject thiz,
                                                               jint width, jint height, jint fps,
                                                               jint bitrate) {
    // TODO: implement native_setVideoEncInfo()
    LOGI("============== setVideoEncInfo width = %d, height = %d ==============", width, height);
    mVideoChannel = new VideoChannel();
    LOGI("============== v->init() ===============");
    mVideoChannel->init();
    if (mVideoChannel != NULL) {
        mVideoChannel->setVideoEncInfo(width, height, fps, bitrate);
    }
}
extern "C"
JNIEXPORT void JNICALL
Java_com_example_myx264test_CameraUtil_native_1sendYUVData(JNIEnv *env, jobject thiz, jbyteArray y,
                                                           jbyteArray u, jbyteArray v, jint ylength,
                                                           jint ulength, jint vlength) {
    // TODO: implement native_sendYUVData()
        LOGI("============== sendYUVData ===============");
    if (mVideoChannel != NULL) {
        LOGI("============== sendYUVData mVideoChannel != NULL ===============");
        jbyte *ydata = env->GetByteArrayElements(y, NULL);
        jbyte *udata = env->GetByteArrayElements(u, NULL);
        jbyte *vdata = env->GetByteArrayElements(v, NULL);

        mVideoChannel->encodeData(ydata, udata, vdata, ylength, ulength, vlength);

        env->ReleaseByteArrayElements(y, ydata,  0);
        env->ReleaseByteArrayElements(u, udata,  0);
        env->ReleaseByteArrayElements(v, vdata,  0);
    }
}
extern "C"
JNIEXPORT void JNICALL
Java_com_example_myx264test_MainActivity_native_1close(JNIEnv *env, jobject thiz) {
    // TODO: implement native_close()
    LOGI("============== native_1close ===============");
    if (mVideoChannel != NULL){
        mVideoChannel->close();
    }
}